import baostock as bs
import pandas as pd

if __name__ == "__main__":
    lg = bs.login()
    print("login rsp code=" + lg.error_code)
    print("login rsp msg=" + lg.error_msg)
    rs = bs.query_history_k_data_plus(
        "sh.600000",
        "date,time,code,open,high,low,close,volume,amount,adjustflag",
        start_date="2023-07-01",
        end_date="2023-07-10",
        frequency="5",
        adjustflag="3",
    )
    print("query_history_k_data_plus rsp code" + rs.error_code)
    print("query_history_k_data_plus rsp msg" + rs.error_msg)

    # 打印结果
    data_list = []
    while (rs.error_code == "0") & rs.next():
        data_list.append(rs.get_row_data())
        print(rs.get_row_data())
    result = pd.DataFrame(data_list, columns=rs.fields)
    print(result)
    print("end ok")
    # 退出
    bs.logout()
